(use-package w3m
  :if (executable-find "w3m")
  ;; :defer 5
  ;; :commands (webjump)
  :hook
  (w3m-mode . qrthi/display-line-numbers-mode-off)
  :bind
  (:map w3m-mode-map
	("& &" . qrthi/w3m-open-current-url-external)
	("'" . qrthi/w3m-open-dead-url-external)
	("C-c C-s" . w3m-history)
	("g" . w3m-reload-this-page)
	("l" . w3m-view-previous-page)
	("q" . kill-this-buffer)
	;; ("p" . w3m-visual-scroll-down)
	;; ("n" . w3m-visual-scroll-up)
	;; ("s" . nil)
	("r" . w3m-view-next-page)
	("I" . w3m-download-this-image)
	;; ("RET" . qrthi/w3m-view-this-url)
	("C-M-j" . w3m-edit-url)
	("C-j" . qrthi/w3m-yank-current-url)
	;; ("C-l" . recenter-top-bottom)
	;; ("M-a" . backward-sentence)
	("M-g o" . w3m-lnum-goto)
	)
  :custom-face
  (w3m-anchor ((t (:inherit link))))
  (w3m-arrived-anchor ((t (:inherit link-visited))))
  (w3m-form ((t (:inherit widget-field :underline t))))
  ;; (w3m-header-line-background ((t (:inherit header-line))))
  ;; (w3m-header-line-content ((t (:inherit info-header-xref))))
  ;; (w3m-header-line-title ((t (:inherit header-line))))
  (w3m-lnum ((t (:inherit w3m-image-anchor))))
  :custom
  ;; (w3m-user-agent "w3m/0.5.3")
  ;; (w3m-user-agent "Mozilla/5.0 (Windows NT 6.1; rv:52.0) Gecko/20100101 Firefox/52.0")
  (w3m-coding-system 'utf-8)
  (w3m-file-coding-system 'utf-8)
  (w3m-file-name-coding-system 'utf-8)
  (w3m-input-coding-system 'utf-8)
  (w3m-output-coding-system 'utf-8)
  (w3m-terminal-coding-system 'utf-8)
  (w3m-confirm-leaving-secure-page nil)
  (w3m-default-directory (expand-file-name "w3m" no-littering-var-directory))
  (w3m-default-save-directory qrthi/directory-downloads)
  (w3m-external-view-temp-directory (expand-file-name "w3m" no-littering-var-directory))
  (w3m-profile-directory (expand-file-name "w3m" no-littering-var-directory))
  ;; (w3m-form-textarea-directory (expand-file-name "w3m/.textarea" no-littering-var-directory))
  (w3m-session-file (expand-file-name "w3m/session" no-littering-var-directory))
  (w3m-cookie-save-cookies nil)
  (w3m-pop-up-frames nil)
  (w3m-pop-up-windows nil)
  (w3m-use-tab nil)
  (w3m-default-display-inline-images t)
  (w3m-resize-images t)
  ;; (w3m-fill-column nil)
  ;; (w3m-make-new-session t)
  (w3m-use-title-buffer-name t)
  (w3m-image-animate-seconds nil)
  (w3m-ignored-image-url-regexp shr-blocked-images)
  ;; (w3m-ignored-image-url-regexp "masteringemacs.org")
  :init
  (defun qrthi/browse-url-w3m (url &optional new-session)
    (interactive (browse-url-interactive-arg "Emacs-w3m URL: "))
    (if new-session
	(w3m-goto-url-new-session url)
      (w3m-goto-url url nil nil nil nil nil nil nil t)))
  (defun qrthi/w3m-open-dead-url-external ()
    (interactive)
    (call-interactively 'w3m-process-stop)
    (goto-char (point-min))
    (if (search-forward-regexp "Reading " nil t)
	(qrthi/browse-url-graphical-browser (thing-at-point 'url))))
  (defun qrthi/w3m-open-current-url-external ()
    "Open current URL in graphical browser"
    (interactive)
    (qrthi/browse-url-graphical-browser w3m-current-url))
  (defun qrthi/w3m-yank-current-url ()
    "Open current URL in graphical browser"
    (interactive)
    (kill-new w3m-current-url)
    (message w3m-current-url))
  :config
  (if (not (file-exists-p w3m-default-directory))
      (mkdir w3m-default-directory)))
